package com.taichuan.selfcheck.utils;

import android.content.Context;
import android.os.Build;
import android.os.StatFs;
import android.os.storage.StorageManager;
import android.text.TextUtils;
import android.util.Log;
import com.taichuan.selfcheck.entity.SdCardInfo;
import com.taichuan.tcutils.common.SDCardUtils;
import com.taichuan.tcutils.taichuan.SystemUtils;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;

/**
 * 该类主要用于
 *
 * @author CamelLuo
 * @version 2021/6/29
 */
public class SdCardInfoUtils {
    private static final String TAG = SdCardInfoUtils.class.getSimpleName();

    /**
     * 获取设备中的外置SD 卡信息
     *
     * @param context
     * @return
     */
    public static List<SdCardInfo> getSdCardInfo(Context context) {
        if (SystemUtils.isA64() || SystemUtils.isRk3399()) {
            try {
                StorageManager storageManager = (StorageManager) context.getSystemService(Context.STORAGE_SERVICE);

                Class storeManagerClazz = Class.forName("android.os.storage.StorageManager");

                Method getVolumesMethod = storeManagerClazz.getMethod("getVolumes");

                List<?> volumeInfos = (List<?>) getVolumesMethod.invoke(storageManager);

                Class volumeInfoClazz = Class.forName("android.os.storage.VolumeInfo");

                Method getTypeMethod = volumeInfoClazz.getMethod("getType");
                Method getFsUuidMethod = volumeInfoClazz.getMethod("getFsUuid");

                Field fsTypeField = volumeInfoClazz.getDeclaredField("fsType");
                Field fsLabelField = volumeInfoClazz.getDeclaredField("fsLabel");
                Field pathField = volumeInfoClazz.getDeclaredField("path");
                Field internalPath = volumeInfoClazz.getDeclaredField("internalPath");

                if (volumeInfos != null) {
                    List<SdCardInfo> sdCardInfoList = new ArrayList<>();

                    for (Object volumeInfo : volumeInfos) {
                        String uuid = (String) getFsUuidMethod.invoke(volumeInfo);
                        if (uuid != null) {
                            String fsTypeString = (String) fsTypeField.get(volumeInfo);
                            String fsLabelString = (String) fsLabelField.get(volumeInfo);
                            String pathString = (String) pathField.get(volumeInfo);
                            String internalPathString = (String) internalPath.get(volumeInfo);
                            StatFs statFs = new StatFs(internalPathString);
                            long availableSize = statFs.getAvailableBytes();
                            long totalSize = statFs.getTotalBytes();

                            //构建实体类
                            SdCardInfo sdCardInfo = new SdCardInfo(uuid, fsTypeString, fsLabelString, pathString, internalPathString, availableSize, totalSize);
                            sdCardInfoList.add(sdCardInfo);
                        }
                    }

                    return sdCardInfoList;
                } else {
                    Log.e(TAG, "getSdCardInfo: 反射获取Sd卡相关信息失败，获取VolumeInfo 类实例对象为null");
                }
            } catch (Exception e) {
                e.printStackTrace();
                Log.e(TAG, "getSdCardInfo: 反射获取Sd卡相关信息异常： " + e.getMessage());
                return null;
            }
        }else {         //A33设备
            String sdCardPath = SDCardUtils.getSecondaryStoragePath(context);
            if (!TextUtils.isEmpty(sdCardPath)){
                StatFs statFs = new StatFs(sdCardPath);
                long totalSize = -1;
                long availableSize = -1;
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR2) {
                    totalSize = statFs.getBlockCountLong() * statFs.getBlockSizeLong();
                    availableSize = statFs.getAvailableBlocksLong() * statFs.getBlockSizeLong();
                } else {
                    totalSize = statFs.getBlockCount() * statFs.getBlockSize();
                    availableSize = statFs.getAvailableBlocks() * statFs.getBlockSize();
                }

                List<SdCardInfo> sdCardInfoList = new ArrayList<>();
                //构建实体类
                SdCardInfo sdCardInfo = new SdCardInfo("A33设备无法获取", "A33设备无法获取", "A33设备无法获取",
                        sdCardPath, "A33设备无法获取", availableSize, totalSize);
                sdCardInfoList.add(sdCardInfo);
                return sdCardInfoList;
            }

        }
        return null;
    }
}
